using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.TeamFoundation.VersionControl.Client;
using Ltf.Remoting.Operations;
using Ltf.Remoting.TeamFoundation;
using Ltf.Remoting.TFAccess;

namespace Ltf.MonitorLizard.Forms
{
	public partial class TFGetOptions : Form
	{
		public Workspace workspace;
		private int deletionId;
		private TFServerConnection connection;

		public TFGetOptions(Workspace workspace, string repoPath, int version, string filePath)
		{
			InitializeComponent();
			connection = ServerConnections.GetConnectionForPath(filePath);
			this.workspace = workspace;
			string name;
			int v;
			LizardFileAccess.GetNameAndVersionFromPath(repoPath,out name, out v, out deletionId);
			repoPath = name;
			if (v != 0)
				version = v;
			txtRepoPath.Text = repoPath;
			txtVersion.Text = version.ToString();
			if (version == 0)
				txtVersion.Text = "Latest";
			if (!string.IsNullOrEmpty(filePath))
				txtFilePath.Text = filePath;
			txtFilePath.Enabled = btnPathPicker.Enabled = string.IsNullOrEmpty(filePath);
		}

		private void btnClose_Click(object sender, EventArgs e)
		{
			Close();
		}

		private void btnPathPicker_Click(object sender, EventArgs e)
		{
			FolderBrowserDialog fbd = new FolderBrowserDialog();
			fbd.ShowNewFolderButton = true;
			if (Directory.Exists(txtFilePath.Text))
			{
				fbd.SelectedPath = txtFilePath.Text;
			}
			else
			{
				fbd.SelectedPath = string.Empty;
			}
			try
			{
				fbd.ShowDialog();
			}
			catch (Exception ex)
			{
				string s = fbd.SelectedPath;
			}
			txtFilePath.Text = fbd.SelectedPath;
		
		}

		private void btnCreate_Click(object sender, EventArgs e)
		{
			string local=txtFilePath.Text;
			string folder = Path.GetDirectoryName(local);
			if (!Directory.Exists(folder))
			{
				DialogResult dr = MessageBox.Show("The selected working copy path does not exist. Would you like to create it.",
					"Path Does Not Exist", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
				if (dr == DialogResult.Cancel)
					return;
				try
				{
					Directory.CreateDirectory(folder);
					Lizardenator liz = new Lizardenator();
					liz.Lizardenate(folder, false);
					string tp = workspace.GetTeamProjectForLocalPath(folder).ServerItem;
					string serverRoot = workspace.GetWorkingFolderForServerItem(tp).LocalItem;
					while (serverRoot != folder)
					{
						folder = Path.GetDirectoryName(folder);
						if (!Directory.Exists(connection.GetMetaFolder(folder)))
							liz.Lizardenate(folder,false);
					}
				}
				catch (Exception ex)
				{
					MessageBox.Show(string.Format("Creation of the working folder failed with the following message:\r\n{0}", ex.Message),
						"Create Path Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
					return;
				}
			}


			WorkingFolder wf = workspace.TryGetWorkingFolderForLocalItem(local);
			string existingmap = null;
			if (wf!=null)
				existingmap=wf.ServerItem;
			if (existingmap!=null && existingmap!=txtRepoPath.Text)
			{
				MessageBox.Show("The local path has a different repository mapping in this workspace.", "Invalid File Path");
				return;
			}
			WorkspaceOperators wso = new WorkspaceOperators(connection, workspace);
			if (existingmap == null)
			{
				DialogResult dr = MessageBox.Show(string.Format("Do you wish to create the new mapping for the repository item:\r\n{0}\r\nto:\r\n{1} ?",
					txtRepoPath.Text, local), "Create New Mapping ?",
					MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
				if (dr == DialogResult.Cancel)
					return;
				wso.CreateMapping(txtRepoPath.Text, local);
			}
			RecursionType rt = RecursionType.Full;
			string t = cmbRecursion.Text;
			switch (t)
			{
				case "None":
					rt=RecursionType.None;
					break;
				case "One Level":
					rt=RecursionType.OneLevel;
					break;
				case "Full":
					rt=RecursionType.Full;
					break;
			}

			VersionSpec vs = VersionSpec.Latest;
			int v = 0;
			if (int.TryParse(txtVersion.Text, out v))
				vs = new ChangesetVersionSpec(v);
			wso.Get(new string[] { txtRepoPath.Text }, vs,rt, chbOverWrite.Checked, chbForceGet.Checked);
		}


	}
}